import java.util.*;
import java.io.*;

public class charFrequency{
  
  int store = 0;
  int j = 0, length = 0, value = 0;
  char c = ' ';
  int temp;
  int[] text = new int[200];
  int[] frequency = new int[128];
  int[] data = new int[200];
  HeapNode node = new HeapNode();
  Combine comb = new Combine();
  Queue q = new Queue();
  
  //Input the desired file intro the class's constructor
  public charFrequency(String str) {
    CharReader read = new CharReader(str);
    Sorting sort = new Sorting();
    
    //Stores the ASCII value of the character being read into an array.
    int i;
    for(i = 0; store != -1; i++) {
      try {
        store = read.readNextChar();
      } catch(IOException e) {
        return;
      }
      if(store != 0)
        text[i] = store;
    }
    text = Arrays.copyOf(text, i - 1);
    read.closeFile();
    
    //Sorts the array
    sort.quickSort(text);
  }
  
  //Stores the different ASCII values intro their own array
  public void search() {
    Object allArrays[] = new Object[50];
    int j = 0;
    //Will take each character in the text, convert it into a character, and store the same characters
    //into their own array "data" that has to be passed on before the loop starts again, or else it will be erased.
    while(j < text.length) {
      int i = 0;
      
      int temp = text[j];
      
      int count = 0;
      for(;j < text.length && text[j] == temp; j++) {
        i++;
        data[count] = temp;
        count++;
      }
      data = Arrays.copyOf(data, i);
      frequency[temp] = data.length;
      data = new int[200];
    }
    for(int i = 0; i < frequency.length; i++) {
      if(frequency[i] != 0) {
        //q.enqueue(frequency[i], (char)i); //For letters in the tree
        q.enqueue(frequency[i], i); //For ASCII values in the tree
      }
    }
    while(q.size() > 1) {
      HeapNode left = q.dequeue();
      HeapNode right = q.dequeue();
      node = comb.combine(left, right);
      node.setKey(left.getKey() + right.getKey());
      q.enqueue(node.getKey(), "*");
      System.out.println(node.getNextLeft());
      System.out.println(node.getNextRight());
    }
    System.out.println(node.getNextRight());
  }
}